/**
 * @fileoverview Blocks for the Phaser Blockly modification.
 *
 * @license Copyright 2018 The Coding with Chrome Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author mbordihn@google.com (Markus Bordihn)
 */
 
{namespace cwc.soy.phaser.ArcadePhysicsBlocks autoescape="strict"}


/**
 * Arcade Physics sprite Toolbox
 */
{template .toolbox}
  {@param? opt_show_create_blocks: bool}

  {if $opt_show_create_blocks}

    {call .predefinedBlocks_ /}

    <block type="phaser_physics_arcade_sprite_add">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">player</field>
        </block>
      </value>
      <value name="x">
        <block type="math_number"><field name="NUM">50</field></block>
      </value>
      <value name="y">
        <block type="math_number"><field name="NUM">100</field></block>
      </value>
    </block>

    <block type="phaser_physics_arcade_out_of_bounds">
      <value name="variable">
        <block type="variables_get">
          <field name="VAR">ball</field>
        </block>
      </value>
    </block>
  {/if}

  <block type="phaser_physics_arcade_sprite_adjust">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">player</field>
      </block>
    </value>
    <value name="value">
      <block type="math_number"></block>
    </value>
  </block>

  <block type="phaser_physics_arcade_sprite_adjust_dimension">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">player</field>
      </block>
    </value>
    <value name="width">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
    <value name="height">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
  </block>

  <block type="phaser_physics_arcade_enable">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
  </block>

  <block type="phaser_physics_arcade_sprite_destroy">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
  </block>

  <block type="phaser_physics_arcade_sprite_kill">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">sprite</field>
      </block>
    </value>
  </block>

  {if not $opt_show_create_blocks}

    <block type="phaser_physics_arcade_overlap">
      <value name="object1">
        <block type="variables_get">
          <field name="VAR">ball</field>
        </block>
      </value>
      <value name="object2">
        <block type="variables_get">
          <field name="VAR">block_group</field>
        </block>
      </value>
      <statement name="CODE">
        <block type="phaser_physics_arcade_sprite_destroy">
          <value name="variable">
            <block type="variables_get">
              <field name="VAR">object2</field>
            </block>
          </value>
        </block>
      </statement>
    </block>

    <block type="phaser_physics_arcade_overlap">
      <value name="object1">
        <block type="variables_get">
          <field name="VAR">player</field>
        </block>
      </value>
      <value name="object2">
        <block type="variables_get">
          <field name="VAR">obstacle_group</field>
        </block>
      </value>
    </block>

    <block type="phaser_physics_arcade_collide">
      <value name="object1">
        <block type="variables_get">
          <field name="VAR">ball</field>
        </block>
      </value>
      <value name="object2">
        <block type="variables_get">
          <field name="VAR">paddle</field>
        </block>
      </value>
    </block>

    <block type="phaser_physics_arcade_collide">
      <value name="object1">
        <block type="variables_get">
          <field name="VAR">player</field>
        </block>
      </value>
      <value name="object2">
        <block type="variables_get">
          <field name="VAR">floor</field>
        </block>
      </value>
    </block>

  {/if}
{/template}


/**
 * Predefined arcade physics arcade sprite
 */
{template .predefinedBlocks_ private="true"}
  <block type="phaser_physics_arcade_sprite_ball_add">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">ball</field>
      </block>
    </value>
    <value name="x">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
    <value name="y">
      <block type="math_number"><field name="NUM">100</field></block>
    </value>
    <statement name="CODE">
      {call .attribute_}
        {param property: 'velocity.x' /}
        {param value: 50 /}
        {param content kind="html"}
          {call .attribute_}
            {param property: 'velocity.y' /}
            {param value: 100 /}
            {param content kind="html"}
              {call .attribute_}
                {param property: 'bounce.set' /}
                {param value: 1 /}
                {param content kind="html"}
                  {call .attribute_}
                    {param property: 'collideWorldBounds' /}
                    {param value: 1 /}
                  {/call}
                {/param}
              {/call}
            {/param}
          {/call}
        {/param}
      {/call}
    </statement>
  </block>

  <block type="phaser_physics_arcade_sprite_player_add">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">player</field>
      </block>
    </value>
    <value name="x">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
    <value name="y">
      <block type="math_number"><field name="NUM">200</field></block>
    </value>
    <statement name="CODE">
      <block type="phaser_physics_arcade_sprite_adjust_custom">
        <value name="value">
          <block type="math_number"></block>
        </value>
      </block>
    </statement>
  </block>

  <block type="phaser_physics_arcade_sprite_paddle_add">
    <value name="variable">
      <block type="variables_get">
        <field name="VAR">paddle</field>
      </block>
    </value>
    <value name="x">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
    <value name="y">
      <block type="math_number"><field name="NUM">500</field></block>
    </value>
    <statement name="CODE">
      {call .attribute_}
        {param property: 'immovable' /}
        {param value: 1 /}
        {param content kind="html"}
          {call .attribute_}
            {param property: 'bounce.set' /}
            {param value: 1 /}
            {param content kind="html"}
              {call .attribute_}
                {param property: 'collideWorldBounds' /}
                {param value: 1 /}
              {/call}
            {/param}
          {/call}
        {/param}
      {/call}
    </statement>
  </block>

  <block type="phaser_physics_arcade_sprite_adjust_custom">
    <value name="value">
      <block type="math_number"></block>
    </value>
  </block>


  <block type="phaser_physics_arcade_sprite_adjust_dimension_custom">
    <value name="width">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
    <value name="height">
      <block type="math_number"><field name="NUM">50</field></block>
    </value>
  </block>
{/template}


/**
 * Arcade Sprite attributes
 */
{template .attribute_ private="true"}
  {@param property: string}
  {@param? value: number}
  {@param? content: ?}

  <block type="phaser_physics_arcade_sprite_adjust_custom">
    <field name="property">{$property}</field>
    <value name="value">
      <block type="math_number">
        <field name="NUM">{if $value}{$value}{/if}</field>
      </block>
    </value>
    {if $content}
      <next>{$content}</next>
    {/if}
  </block>
{/template}
